from django.template import loader, Context
from django.http import HttpResponse
from django.core import serializers
from kc.career.models import *

def AppList(request):
	Apps = Application.objects.all()
	
	try:
		action = request.GET['act']
		if 't' in action:
			Apps = Apps.filter(IsApplied = False)
		if 'o' in action: # on_watch
			Apps = Apps.filter(OnWatch = True)
		if 'f' in action: # rejected
			Apps = Apps.filter(OnWatch = False)
		if 'a' in action: # Applied
			Apps = Apps.filter(IsApplied = True)
		if 'r' in action: # Replied
			Apps = Apps.filter(IsReplied = True)
		if 's' in action: # Succeed
			Apps = Apps.filter(IsSucceed = True)
	except:
		pass
	
	status = ''		
	try:
		App_id = int(request.GET['id'])
		App = Apps.get(id = App_id)
		if not App.IsApplied:
			status += 'Hasn\'t Applied'
		elif App.IsReplied:
			if App.IsSucceed:
				status += 'Offer'
			elif not App.OnWatch:
				status += 'Rejected'
			elif App.Second:
				status += 'Passed Second Interview'
			elif App.First:
				status += 'Passed First Interview'
			else:
				status += 'Replied, in Progress'
		else:
			status += 'Hasn\'t Replied'
		print status
		t = loader.get_template("show_app.html")
		c = Context({
						'title':'The Application: ' + App.CompanyName,
						'a': App,
						'status': status,
					})
		return HttpResponse(t.render(c))
	except:
		pass
	
	t = loader.get_template("show_all.html")
	c = Context({
					'title':'The Applications',
					'apps': Apps,
					'status': status,
				})		
	return HttpResponse(t.render(c))